/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package AirLine;

import Database.Flights;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import java.util.*;
import Model.*;
import java.awt.print.Book;
import java.sql.*;
import java.util.ArrayList;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Administrator
 */
public class BookTrip extends HttpServlet{
    @Override
    public void doPost(HttpServletRequest request,
                        HttpServletResponse response)
            throws IOException, ServletException{
        
//        try {
            ArrayList<String> name = new ArrayList<String>(10);
            ArrayList<String> pn = new ArrayList<String>(10);
            //String[] prefer = new String[10];
            ArrayList<String> prefer=new ArrayList<String>();
            int[] preferList;
            HttpSession session = request.getSession();
            ArrayList<Person> passengers = new ArrayList<Person>();
            

            System.out.println("====BookTrip.java=====");
            // get number of passengers
            Integer total = (Integer) session.getAttribute("number");
            int number = total.intValue();
            preferList=new int[number];
            System.out.println("Passenger numbers: " + number);
            
            // get all passengers infomation
            // save into list, passengers
            
            // get all name and ssn
            for (int i = 0; i < number; i++) {
                String argName = "name" + i;
                String argSsn = "number" + i;
                String argPrefer="prefer"+i;
                String tmpName = (String) request.getParameter(argName);
                String tmpSsn  = (String) request.getParameter(argSsn);
                String kolla =  request.getParameter(argPrefer);
                preferList[i]=Integer.valueOf(kolla);
                //System.out.println("GET a person name: " + tmpName + "--" + tmpSsn);
                name.add(tmpName);
                pn.add(tmpSsn);
            }
            // generate the passengers list
            Iterator<String> it1 = name.iterator();
            Iterator<String> it2 = pn.iterator();
            for( int i = 1; it1.hasNext(); i++ ){
                String tempName = it1.next();
                String tempSsn = it2.next();
                System.out.println("P<" + i + "> " + tempName + "--" + tempSsn);
                Person temp = new Person(tempName, tempSsn);
                passengers.add(temp);
            }       
 
            Trip trip = (Trip) session.getAttribute("trip");
            System.out.println("Trip: " + trip.getDepartureAirport() + 
                            " -> " + trip.getDestinationAirport());
            Iterator<Flight> it3 = trip.getFlights().iterator();
            System.out.println(trip.getFlights().size());
            System.out.println("Check Flights here: ");
            while( it3.hasNext() ){
                Flight f = it3.next();
                System.out.println("Flight ID: <" + f.getPlaneId() + ">");
            }
            // no use 5 lines
//            Iterator<Flight> itFlight = trip.getFlights().iterator();
//            while(itFlight.hasNext()){
//                Flight flight = itFlight.next();
//                System.out.println("flight" + flight.getCode());
//            }
//        book(ArrayList<Booking> b)
//        public Booking(Person person,Flight flight,Seat seat)
            
            
            // one booking
            Person person = null;
            Flight flight = null;
            Seat   seat   = null;
            ArrayList<Booking> books = new ArrayList<Booking>();
            Iterator<Person> itPerson = passengers.iterator();
            
            for(Flight f: trip.getFlights())
            {
                for(int i=0; i<passengers.size(); i++)
                {
                    Person p=passengers.get(i);
                    int type=preferList[i];
                    
                    for(Seat s: f.getFreeSeats())
                    {
                       //System.out.println("typen: "+s.getType());
                        //if(!s.isBooked() && s.getType()==type)
                        if(!s.isBooked())
                        {
                                                Booking booking = new Booking(p, f,  s);
                                                books.add(booking);
                                                s.setBooked(true);
                                                System.out.println("bokar person med seat: "+p.getName()+"/ "+s.getColumn()+"|"+s.getRow());
                                                
                        }
                        if(s.isBooked())
                            break;
                    }
                }
                
                
            }
//            while( itPerson.hasNext() ){
//                Person p = itPerson.next();
//                Iterator<Flight> itFlight = trip.getFlights().iterator();
//                while( itFlight.hasNext() ){
//                    Flight f = itFlight.next();
//                    //Seat s = new Seat(false, Seat.ISLE_TYPE, f.getPlane().getId(), 20, 15);
//                    Set<Seat> s = f.getFreeSeats();
//                    
//                    
//                    Iterator<Seat> aSeat = s.iterator();
//                    Seat seat2=aSeat.next();
//                    Booking booking = new Booking(p, f,  aSeat.next());
//                    books.add(booking);
//                }
//            }
            Model model = (Model )session.getAttribute("model");
            if( model == null ){
                System.out.println("XXX no model XXXX");
            }
            model.book(books);

            //System.out.println("*********" + request.getParameter("name0"));
            //
//            Flights flights = new Flights();
//            TreeSet list = flights.getFlightsList();
//            Iterator it = list.iterator();
//            
//            Person p = new Person(name.get(0), pn.get(0));
//            System.out.println(p.getName() +"--"+ p.getSSN());
//            if(it.hasNext()){
//                Flight flight = (Flight) it.next();
//                System.out.println(flight);
//                Seat seat = flight.getFreeSeats().iterator().next();
//                Booking booking = new Booking(p, flight, seat );
//            }

            request.setAttribute("names", name);
            request.setAttribute("numbers", pn);
            request.setAttribute("prefers", prefer);
            request.setAttribute("total", number);
            RequestDispatcher view = request.getRequestDispatcher("bookresult.jsp");
            view.forward(request, response);
//        } catch (SQLException ex) {
//            Logger.getLogger(BookTrip.class.getName()).log(Level.SEVERE, null, ex);
//        }
        
    }

}
